package com.lijin.web;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lijin.entity.User;
import com.lijin.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by gold-jx on 2017/5/31.
 */
@RestController
@RequestMapping("/user")
@Api("用户user相关接口")
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation("添加用户接口，带返回值")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "name", value = "姓名", dataType = "String", required = true, paramType = "query"),
            @ApiImplicitParam(name = "age", value = "年龄", dataType = "INT", required = true, paramType = "query"),
            @ApiImplicitParam(name = "sex", value = "性别", dataType = "String", required = true, paramType = "query")
    })
    public String insert(
            @RequestParam(value = "name", required = true) String name,
            @RequestParam(value = "age", required = true) Integer age,
            @RequestParam(value = "sex", required = true) String sex
    ) {
        try {
            Assert.notNull(name, "姓名不能为空");
            Assert.notNull(age, "年龄不能为空");
            Assert.notNull(sex, "性别不能为空");
            User user = new User();
            user.setName(name);
            user.setAge(age);
            user.setSex(sex);
            int number=userService.insertUser(user);
            return "SUCCESS:"+number;

        } catch (Exception e) {
            e.printStackTrace();
            return "FAIL";
        }

    }


    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ApiOperation("添加用户接口")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "name", value = "姓名", dataType = "String", required = true, paramType = "query"),
            @ApiImplicitParam(name = "age", value = "年龄", dataType = "INT", required = true, paramType = "query"),
            @ApiImplicitParam(name = "sex", value = "性别", dataType = "String", required = true, paramType = "query")
    })
    public String add(
            @RequestParam(value = "name", required = true) String name,
            @RequestParam(value = "age", required = true) Integer age,
            @RequestParam(value = "sex", required = true) String sex
    ) {
        try {
            Assert.notNull(name, "姓名不能为空");
            Assert.notNull(age, "年龄不能为空");
            Assert.notNull(sex, "性别不能为空");
            User user = new User();
            user.setName(name);
            user.setAge(age);
            user.setSex(sex);
            userService.addUser(user);
            return "SUCCESS";

        } catch (Exception e) {
            e.printStackTrace();
            return "FAIL";
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ApiOperation("根据用户名删除")
    @ApiImplicitParam(name = "name", value = "姓名", dataType = "String", required = true, paramType = "query")
    public String deleteUserByName(@RequestParam(value = "name", required = true) String name){

        try {
            Assert.notNull(name, "姓名不能为空");
            userService.deleteUserByName(name);
            return "SUCCESS";
        }catch (Exception e){
            e.printStackTrace();
            return "FAIL";
        }

    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation("修改用户接口")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "name", value = "姓名", dataType = "String", required = true, paramType = "query"),
            @ApiImplicitParam(name = "age", value = "年龄", dataType = "INT", required = true, paramType = "query"),
            @ApiImplicitParam(name = "sex", value = "性别", dataType = "String", required = true, paramType = "query")
    })
    public String updateUserByName(
            @RequestParam(value = "name", required = true) String name,
            @RequestParam(value = "age", required = true) Integer age,
            @RequestParam(value = "sex", required = true) String sex
    ){
        try {
            Assert.notNull(name, "姓名不能为空");
            Assert.notNull(age, "年龄不能为空");
            Assert.notNull(sex, "性别不能为空");
            User user = new User();
            user.setName(name);
            user.setAge(age);
            user.setSex(sex);
            userService.updateUserByName(user);
            return "SUCCESS";

        }catch (Exception e){
            e.printStackTrace();
            return "FAIL";
        }

    }


    @RequestMapping(value = "/findByName", method = RequestMethod.POST)
    @ApiOperation("根据姓名查询用户接口")
    @ApiImplicitParam(name = "name", value = "姓名", dataType = "String", required = true, paramType = "query")
    public User findUserByName(@RequestParam(value = "name", required = true) String name){
        try {
            Assert.notNull(name, "姓名不能为空");
            User user=userService.findUserByName(name);
            return user;

        }catch (Exception e){
            e.printStackTrace();
            return null;

        }

    }

    @RequestMapping(value = "/findAll", method = RequestMethod.POST)
    @ApiOperation("查询所有用户接口")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "pageNum", value = "当前页", dataType = "Integer", required = false, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页容量", dataType = "Integer", required = false, paramType = "query")
    })
    public PageInfo findUserAll(
            @RequestParam(value = "pageNum", required = false) Integer pageNum,
            @RequestParam(value = "pageSize", required = false) Integer pageSize
    ){
        try {
            pageNum = pageNum == null ? 1 : pageNum;
            pageSize = pageSize == null ? 10 : pageSize;
            PageHelper.startPage(pageNum, pageSize);
            List<User> list=userService.findUserAll();
            PageInfo pageInfo = new PageInfo(list);
            Page page = (Page) list;
            return pageInfo;

        }catch (Exception e){
            e.printStackTrace();
            return null;
        }

    }








}
